/*This script generates results at the county level data found in the main text;
Table 2
Table 3

Table C2 data supports Table 3 and is produced here.
*/


cd "$data"
set more off

*Bring in the county data
use US_county_2010_Turbines_Irrigation_JAERE.dta, replace

*Globals drawn on
global xcontrols trans_length_a pop_density elev elev_std soil stream_3_15 tmean ppt lat_county lon_county
global xwind ave_wind_class max_wind_class

global sumstat share_og share_og_neg20  ave_wind_class max_wind_class cpis irrigated_a crop_a farm_a t_cap_a first_year ave_year trans_length_a pop_density elev elev_std soil stream_3_15 tmean ppt pback sback

/*Table 2*/

foreach rest in samp og central og_ring non_og beyond_samp {
	estpost tabstat $sumstat if  `rest'==1, columns(statistics) statistics(mean)
	estadd matrix r=e(mean)
	eststo diff_`rest'
	estpost tabstat lon_county if  `rest'==1, columns(statistics) statistics(count)
	estadd matrix r=e(count)
	eststo tab_diff_c_`rest'
	estpost tabstat pback if  `rest'==1, columns(statistics) statistics(count)
	estadd matrix r=e(count)
	eststo tab_diff_c_set_`rest'
	}


	esttab diff_samp diff_og diff_central diff_og_ring diff_non_og diff_beyond_samp tab_diff_c_samp  tab_diff_c_og tab_diff_c_central tab_diff_c_og_ring tab_diff_c_non_og tab_diff_c_beyond_samp tab_diff_c_set_samp  tab_diff_c_set_og tab_diff_c_set_central tab_diff_c_set_og_ring tab_diff_c_set_non_og tab_diff_c_set_beyond_samp ///
 using "../results/tables/tab2_county_balance.csv", replace label main(r)  nostar  mtitles("Sample" "Ogallala" "Central Ogallala" "Ogallala Ring" "Beyond Ogallala" "Beyond Sample") nonote noobs

/*Table 3*/
local i=1

tobit t_cap_a irrigated_a farm_a crop_a $xwind $xcontrols i.statefp if share_og_pos100!=., ll(0) vce(cl cluster_km)
	estadd scalar Nclus =  e(N_clust)
	qui sum t_cap_a if e(sample)==1
	estadd scalar mean = r(mean)
		
	qui tab statefp if e(sample)==1
	estadd scalar df_a = r(r)
	
	qui sum t_cap_a if e(sample)==1 & t_cap_a>0
	local ey0=r(mean)
	estadd scalar ey0 = `ey0'
	
	estadd scalar prY0 = e(N_unc)/e(N)
		
	margins, dydx(irrigated_a)  predict(pr(0,.)) predict(ystar(0,.)) predict(ystar(.,.)) predict(e(0,.)) 
	local dyPr=el(r(b),1,1)
	estadd scalar dyPr = `dyPr'
	local dyy0=el(r(b),1,2)
	estadd scalar dyy0 = `dyy0'
	estadd scalar dyy=e(N_unc)/e(N)*`dyy0'+`ey0'*`dyPr'
	
	estimates store t_cap_`i'

local i=`i'+1
tobit t_cap_a cpis farm_a crop_a $xwind $xcontrols i.statefp if share_og_pos100!=. , ll(0) vce(cl cluster_km)
	estadd scalar Nclus =  e(N_clust)
	qui sum t_cap_a if e(sample)==1
	estadd scalar mean = r(mean)
	
	qui tab statefp if e(sample)==1
	estadd scalar df_a = r(r)
	
	qui sum t_cap_a if e(sample)==1 & t_cap_a>0
	local ey0=r(mean)
	estadd scalar ey0 = `ey0'	
	
	estadd scalar prY0 = e(N_unc)/e(N)
	
	margins, dydx(cpis)  predict(pr(0,.)) predict(ystar(0,.)) predict(ystar(.,.)) predict(e(0,.)) 
	local dyPrCPIS=el(r(b),1,1)
	estadd scalar dyPrCPIS = `dyPrCPIS'
	local dyy0CPIS=el(r(b),1,2)
	estadd scalar dyy0CPIS = `dyy0CPIS'
	estadd scalar dyyCPIS=e(N_unc)/e(N)*`dyy0CPIS'+`ey0'*`dyPrCPIS'
	
	estimates store t_cap_`i'

local i=`i'+1
tobit t_cap_a irrigated_a cpis farm_a crop_a $xwind $xcontrols i.statefp if share_og_pos100!=. , ll(0) vce(cl cluster_km)
	estadd scalar Nclus =  e(N_clust)
	qui sum t_cap_a if e(sample)==1
	estadd scalar mean = r(mean)
	
	qui tab statefp if e(sample)==1
	estadd scalar df_a = r(r)
	
	qui sum t_cap_a if e(sample)==1 & t_cap_a>0
	local ey0=r(mean)
	estadd scalar ey0 = `ey0'
	
	estadd scalar prY0 = e(N_unc)/e(N)
	
	margins, dydx(irrigated_a)  predict(pr(0,.)) predict(ystar(0,.)) predict(ystar(.,.)) predict(e(0,.)) 
	local dyPr=el(r(b),1,1)
	estadd scalar dyPr = `dyPr'
	local dyy0=el(r(b),1,2)
	estadd scalar dyy0 = `dyy0'
	estadd scalar dyy=e(N_unc)/e(N)*`dyy0'+`ey0'*`dyPr'

	margins, dydx(cpis)  predict(pr(0,.)) predict(ystar(0,.)) predict(ystar(.,.)) predict(e(0,.)) 
	local dyPrCPIS=el(r(b),1,1)
	estadd scalar dyPrCPIS = `dyPrCPIS'
	local dyy0CPIS=el(r(b),1,2)
	estadd scalar dyy0CPIS = `dyy0CPIS'
	estadd scalar dyyCPIS=e(N_unc)/e(N)*`dyy0CPIS'+`ey0'*`dyPrCPIS'
	
	estimates store t_cap_`i'

local i = 1
foreach irr in "irrigated_a" "cpis" "irrigated_a cpis" {
local i=`i'+1
	reg first_year `irr' farm_a crop_a $xwind $xcontrols i.statefp if share_og_pos100!=. & t_cap_a>0,  cl(cluster_km)
		estadd scalar Nclus =  e(N_clust)
		qui sum first_year if e(sample)==1
		estadd scalar mean = r(mean)	
		qui tab statefp if e(sample)==1
		estadd scalar df_a = r(r)
	
	estimates store first_year_`i'
	
	local i = `i' + 1
	}

esttab t_cap_* first_year_*  using "$results/tables/tab3_County_Irr_CPIS.csv", label  keep( irrigated_a cpis ave_wind_class max_wind_class trans_length_a) order(irrigated_a cpis ave_wind_class max_wind_class trans_length_a) star(* 0.1 ** 0.05 *** 0.01)  ar2 se(a3) b(a3) scalars("r2_p Pseudo R-squared" "N_lc Censored Observations" "mean Mean DV" "df_a Fixed Effect Count" "Nclus N Clusters" "prY0 Pr(Y>0)" "ey0 E[y|y>0]" "dyPr d(pr(y>0))/dx(Irr)" "dyy0 d(y|y>0)/dx(Irr)" "dyy d(y)/dx(Irr)" "dyPrCPIS d(pr(y>0))/dx(CPIS)" "dyy0CPIS d(y|y>0)/dx(CPIS)" "dyyCPIS d(y)/dx(CPIS)") replace 
